#include <iostream>
using namespace std;

struct TreeNode {
  int val;
  TreeNode* left;
  TreeNode* right;
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

bool hasPathSum(TreeNode* root, int sum) {
  if (!root) {
    return false;
  }

  if (!root->left && !root->right && root->val == sum) {
    return true;
  }

  return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}

int main() {
  TreeNode* root = new TreeNode(5);
  root->left = new TreeNode(4);
  root->right = new TreeNode(8);
  root->left->left = new TreeNode(11);
  root->left->left->left = new TreeNode(7);
  root->left->left->right = new TreeNode(2);
  root->right->left = new TreeNode(13);
  root->right->right = new TreeNode(4);
  root->right->right->right = new TreeNode(1);

  int targetSum = 22;

  bool result = hasPathSum(root, targetSum);

  if (result) {
    cout << "存在路径总和等于目标值" << endl;
  } else {
    cout << "不存在路径总和等于目标值" << endl;
  }

  return 0;
}